package com.deshan.websys.api.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.deshan.websys.api.utils.CommonResult;
import com.deshan.websys.code.model.SmsHomeAdvertise;
import com.deshan.websys.code.service.SmsHomeAdvertiseService;

import cn.hutool.core.util.ArrayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

@Api(tags = "轮播图后台管理")
@RestController
@RequestMapping("/manage/advertise")
@AllArgsConstructor
public class AdvertiseController {

	private final SmsHomeAdvertiseService advertiseService;

	@ApiOperation("创建和修改")
	@PostMapping("/save")
	public CommonResult saveAdvertise(@RequestBody SmsHomeAdvertise advertise) {
		advertise.setPageType(2);
		return advertiseService.saveOrUpdate(advertise) ? CommonResult.success() : CommonResult.failed("保存失败");
	}

	@ApiOperation("删除")
	@DeleteMapping("/delete/{id}")
	public CommonResult deleteAdvertise(@PathVariable("id") String id) {
		return advertiseService.removeById(id) ? CommonResult.success() : CommonResult.failed("删除失败");
	}

	@ApiOperation("批量删除")
	@DeleteMapping("/delete/{ids}")
	public CommonResult batchDeleteAdvertise(@PathVariable("ids") String[] ids) {
		if (ArrayUtil.isNotEmpty(ids)) {
			for (String id : ids) {
				deleteAdvertise(id);
			}
		}
		return CommonResult.success();
	}

	@ApiOperation("根据ID查询轮播图")
	@GetMapping("/getone")
	public CommonResult<SmsHomeAdvertise> getAdvertise(@RequestParam("id") String id) {
		return CommonResult.success(advertiseService.getById(id));
	}

	@ApiOperation("查询当前法人下的所有轮播图")
	@GetMapping("/list/{legalCode}")
	public CommonResult<List<SmsHomeAdvertise>> listAdvertise(@PathVariable("legalCode") String legalCode) {
		return CommonResult.success(advertiseService
				.list(Wrappers.lambdaQuery(SmsHomeAdvertise.class).eq(SmsHomeAdvertise::getLegalCode, legalCode)));
	}
}
